/* $Id: EditorsFactory.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.editors;

import alt.djudge.frontend.client.ui.admin.Layout;
import alt.djudge.frontend.shared.dto.admin.ContestEntryDTO;
import alt.djudge.frontend.shared.dto.admin.ContestLanguageEntryDTO;
import alt.djudge.frontend.shared.dto.admin.ContestProblemEntryDTO;
import alt.djudge.frontend.shared.dto.admin.ContestUserEntryDTO;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;
import alt.djudge.frontend.shared.dto.admin.LanguageEntryDTO;
import alt.djudge.frontend.shared.dto.admin.LocaleEntryDTO;
import alt.djudge.frontend.shared.dto.admin.ProblemEntryDTO;
import alt.djudge.frontend.shared.dto.admin.ProblemStatementEntryDTO;
import alt.djudge.frontend.shared.dto.admin.UserEntryDTO;

public class EditorsFactory
{
	static ListBoxEditor contestEditor;
	
	static ListBoxEditor languageEditor;
	
	static ListBoxEditor userEditor;
	
	static ListBoxEditor problemEditor;
	
	static ListBoxEditor localeEditor;
	
	public static ListBoxEditor getContestEditor()
	{
		if (contestEditor == null)
		{
			contestEditor = new ListBoxEditor(0, 2);
			Layout.adminService.getEntries(ContestEntryDTO.class.getName(), -1, contestEditor.getCallback());
		}
		return contestEditor;
	}
	
	public static ListBoxEditor getLanguageEditor()
	{
		if (languageEditor == null)
		{
			languageEditor = new ListBoxEditor(0, 2);
			Layout.adminService.getEntries(LanguageEntryDTO.class.getName(), -1, languageEditor.getCallback());
		}
		return languageEditor;
	}
	
	public static ListBoxEditor getLocaleEditor()
	{
		if (localeEditor == null)
		{
			localeEditor = new ListBoxEditor(0, 2);
			Layout.adminService.getEntries(LocaleEntryDTO.class.getName(), -1, localeEditor.getCallback());
		}
		return localeEditor;
	}
	
	public static ListBoxEditor getUserEditor()
	{
		if (userEditor == null)
		{
			userEditor = new ListBoxEditor(0, 2);
			Layout.adminService.getEntries(UserEntryDTO.class.getName(), -1, userEditor.getCallback());
		}
		return userEditor;
	}
	
	public static ListBoxEditor getProblemEditor()
	{
		if (problemEditor == null)
		{
			problemEditor = new ListBoxEditor(0, 1);
			Layout.adminService.getEntries(ProblemEntryDTO.class.getName(), -1, problemEditor.getCallback());
		}
		return problemEditor;
	}
	
	public static CustomEditor getEditor(Class<? extends AdminDTOEntry> entryClass, int fieldIndex)
	{
		if (entryClass.equals(ContestLanguageEntryDTO.class))
		{
			if (fieldIndex == 1)
			{
				return getContestEditor();
			}
			else if (fieldIndex == 2)
			{
				return getLanguageEditor();
			}
		}
		else if (entryClass.equals(ContestProblemEntryDTO.class))
		{
			if (fieldIndex == 1)
			{
				return getContestEditor();
			}
			else if (fieldIndex == 2)
			{
				return getProblemEditor();
			}
		}
		else if (entryClass.equals(ContestUserEntryDTO.class))
		{
			if (fieldIndex == 1)
			{
				return getContestEditor();
			}
			else if (fieldIndex == 2)
			{
				return getUserEditor();
			}
		}
		else if (entryClass.equals(LocaleEntryDTO.class))
		{
			if (fieldIndex == 1)
			{
				return getLocaleEditor();
			}
		}		
		else if (entryClass.equals(ProblemStatementEntryDTO.class))
		{
			if (fieldIndex == 1)
			{
				return getLocaleEditor();
			}
			else if (fieldIndex == 2)
			{
				return getProblemEditor();
			}
			else if (fieldIndex == 3)
			{
				return new TextAreaEditor();
			}
		}		
		return new TextBoxEditor();
	}
}
